package data.framework.service;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import data.framework.dao.hibernate.BaseHibernateDao;
import data.framework.dao.jdbc.BaseJdbcDaoSupport;
import data.framework.dao.mybatis.BaseMyBatisDao;
import data.framework.utils.BeanReflectionUtils;

public abstract class AbstractService<E> {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 实体对象类定义
	 */
	protected Class<E> entityClass;
	
	@Autowired
	private BaseHibernateDao<E> baseHibernateDao;
	@Autowired
	private BaseMyBatisDao<E> baseMyBatisDao;

	 @Autowired
	 private BaseJdbcDaoSupport<E> jdbcDao;
	
	public AbstractService(){
		entityClass = BeanReflectionUtils.getSuperClassGenricType(getClass());
	}
	
	protected BaseHibernateDao<E> getHibernateDao() {
		// hibernateDao.setEntityClass(entityClass);
		return baseHibernateDao;
	}

	protected BaseMyBatisDao<E> getMyBatisDao() {
		// myBatisDao.setEntityClass(entityClass);
		return baseMyBatisDao;
	}

	protected BaseJdbcDaoSupport<E> getJdbcDao() {
		// jdbcDao.setEntityClass(entityClass);
		return jdbcDao;
	}

	public E load(Serializable id) {
		return this.getHibernateDao().get(id);
	}

	public void save(E entity) {
		this.getHibernateDao().save(entity);
	}

	public void update(E entity) {
		this.getHibernateDao().update(entity);
	}

	public void delete(E entity) {
		this.getHibernateDao().delete(entity);
	}

	public void delete(Serializable id) {
		this.getHibernateDao().delete(id);
	}
}
